﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using G.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;
using G.Navigate;

namespace G.WeiBo.View.ChildWindows
{
    public partial class BrowserBigImageChildWindow : ChildWindow
    {
        public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(Uri), typeof(BrowserBigImageChildWindow), new PropertyMetadata(null, (s, e) =>
        {

            BrowserBigImageChildWindow b = s as BrowserBigImageChildWindow;
            //b.web.Source = b.Url;
            if (System.IO.Path.GetExtension(b.Url.OriginalString).ToLower() == ".gif")
            {
                b.SetGifImg();
            }
            else
            {
                b.SetNormalImg();
            }
        }));
        //CompositeTransform transform;
        private void SetGifImg()
        {
            //this.gifimg.UriSource = this.Url;
            this.img.Visibility = Visibility.Collapsed;
            //currentImg = this.gifimg;
        }
        FrameworkElement currentImg = null;
        private void SetNormalImg()
        {
            WebClient client = new WebClient();
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(gifimg_DownloadProgressChanged);
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
            client.OpenReadAsync(this.Url);

            //this.gifimg.Visibility = Visibility.Collapsed;
            currentImg = this.img;
        }
        byte[] imgBuffer = null;
        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                BitmapImage imgSource = new BitmapImage();
                imgBuffer = e.Result.GetBytes();
                imgSource.SetSource(e.Result);
                this.img.Source = imgSource;
                loadingIndicator.IsIndeterminate = false;
                stackPanelPro.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                loadingIndicator.IsIndeterminate = false;
                stackPanelPro.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (!this.IsOpen)
            {
                PhoneApplicationPageHelp.ShowState("图片加载完成，是否显示图片？点击显示", TimeSpan.FromSeconds(4), new SolidColorBrush(Colors.Orange), () =>
                {
                    this.IsOpen = true;
                });
            }
        }
        public BrowserBigImageChildWindow()
        {
            InitializeComponent();
            //transform = new CompositeTransform();
        }
        public Uri Url
        {
            get
            {
                return this.GetValue(UrlProperty) as Uri;
            }
            set
            {
                this.SetValue(UrlProperty, value);
            }
        }
        private void ApplicationBarSave_Click(object sender, EventArgs e)
        {
        }
        private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            initialAngle = transform.Rotation;
            initialScale = transform.ScaleX;
        }
        double initialAngle;
        double initialScale;
        private void OnPinchDelta(object sender, PinchGestureEventArgs e)
        {
            this.transform.CenterX = this.grid.ActualWidth / 2;
            this.transform.CenterY = this.grid.ActualHeight / 2;
            transform.Rotation = initialAngle + e.TotalAngleDelta;
            transform.ScaleX = transform.ScaleY = initialScale * e.DistanceRatio;
        }

        private void OnPinchCompleted(object sender, PinchGestureEventArgs e)
        {

        }
        private void OnDragStarted(object sender, DragStartedGestureEventArgs e)
        {

        }

        private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            transform.TranslateX += e.HorizontalChange;
            transform.TranslateY += e.VerticalChange;
        }

        private void OnDragCompleted(object sender, DragCompletedGestureEventArgs e)
        {

        }

        private void OnDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            this.transform.ScaleX = this.transform.ScaleY = 1;
            transform.TranslateX = transform.TranslateY = 0;
            transform.Rotation = 0;
        }

        //private void img_ImageOpened(object sender, RoutedEventArgs e)
        //{
        //    loadingIndicator.StopAnimation();
        //}

        private void img_ImageOpened(object sender, EventArgs e)
        {
            loadingIndicator.IsIndeterminate = false;
            stackPanelPro.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void gifimg_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            this.txtPro.Text = e.ProgressPercentage + "%";
        }


        private void btnSavePictrue_Click(object sender, RoutedEventArgs e)
        {
            MediaLibrary mediaLibrary = new MediaLibrary();
            if (imgBuffer != null)
            {
                mediaLibrary.SavePicture(DateTime.Now.ToShortTimeString(), imgBuffer);
                G.Navigate.PhoneApplicationPageHelp.ShowState("图片保存成功");
            }
        }

    }
}
